package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoInformationCopy1;
import com.muyu.danmo.domain.DanmoInformationVideo;
import com.muyu.danmo.mapper.DanmoInformationVideoMapper;
import com.muyu.danmo.service.IDanmoInformationVideoService;
import com.muyu.remote.RemoteAddRessFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 个人信息视频集Service业务层处理
 *
 * @author yangle
 * @date 2024-12-18
 */
@Service
public class DanmoInformationVideoServiceImpl implements IDanmoInformationVideoService {
    @Autowired
    private DanmoInformationVideoMapper danmoInformationVideoMapper;

    /**
     * 查询个人信息视频集
     *
     * @param id 个人信息视频集主键
     * @return 个人信息视频集
     */
    @Override
    public DanmoInformationVideo selectDanmoInformationVideoById(Long id) {
        return danmoInformationVideoMapper.selectDanmoInformationVideoById(id);
    }

    /**
     * 查询个人信息视频集列表
     *
     * @param danmoInformationVideo 个人信息视频集
     * @return 个人信息视频集
     */
    @Override
    public List<DanmoInformationVideo> selectDanmoInformationVideoList(DanmoInformationVideo danmoInformationVideo) {
        return danmoInformationVideoMapper.selectDanmoInformationVideoList(danmoInformationVideo);
    }

    @Autowired
    private DanmoInformationCopy1ServiceImpl danmoInformationCopy1Service;
    /**
     * 新增个人信息视频集
     *
     * @param danmoInformationVideo 个人信息视频集
     * @return 结果
     */
    @Override
    public Result insertDanmoInformationVideo(DanmoInformationVideo danmoInformationVideo) {


            Long userId = danmoInformationVideo.getUserId();
            DanmoInformationCopy1 danmoInformationCopy1 = new DanmoInformationCopy1();
            danmoInformationCopy1.setUserId(userId);
            danmoInformationCopy1.setAssociation(danmoInformationVideo.getAssociation());
            danmoInformationCopy1.setAssociationType(danmoInformationVideo.getAssociationType());
            DanmoInformationCopy1 danmoInformationCopy11 = danmoInformationCopy1Service.selectUserDanmoInformationCopy1(danmoInformationCopy1);
            if (danmoInformationCopy11 == null) {
                return Result.error("请先上传个人信息");
            }
            danmoInformationVideo.setInformationId(danmoInformationCopy11.getId());
            int i = danmoInformationVideoMapper.insertDanmoInformationVideo(danmoInformationVideo);
            return Result.success(i, "添加成功");


    }

    /**
     * 修改个人信息视频集
     *
     * @param danmoInformationVideo 个人信息视频集
     * @return 结果
     */
    @Override
    public Result updateDanmoInformationVideo(DanmoInformationVideo danmoInformationVideo) {
        try {
            int i = danmoInformationVideoMapper.updateDanmoInformationVideo(danmoInformationVideo);
            return Result.success(i, "修改成功");

        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }

    @Autowired
    private RemoteAddRessFileService remoteAddRessFileService;
    /**
     * 批量删除个人信息视频集
     *
     * @param ids 需要删除的个人信息视频集主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoInformationVideoByIds(Long[] ids) {

            for (Long id : ids) {
                DanmoInformationVideo danmoInformationVideo = danmoInformationVideoMapper.selectDanmoInformationVideoById(id);
                remoteAddRessFileService.removeAlyVideo(danmoInformationVideo.getVideoId());
            }
            int deletedCount = danmoInformationVideoMapper.deleteDanmoInformationVideoByIds(ids);
            return Result.success(deletedCount, "删除成功");

    }

    /**
     * 删除个人信息视频集信息
     *
     * @param id 个人信息视频集主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoInformationVideoById(Long id) {
        try {
            int i = danmoInformationVideoMapper.deleteDanmoInformationVideoById(id);
            return Result.success(i, "删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }

    }
}
